import { ref } from "vue";
import { register } from '@/api/user.api';
import { omit } from 'lodash-es';
import useUserStore from '@/store/modules/user';
import { useI18n } from 'vue-i18n';
import emitter from '@/utils/emitter';
export function useFormState() {
  const { t } = useI18n(); 
  const userStore = useUserStore();
  const formRef = ref();
  const formState = ref({
    phoneOrEmail: '',
    sendType: 1,
    password: '',
  });
  const rules = {
    phoneOrEmail: [
      {
        required: true,
        errorMessage: t('register.form.email.validate.required'),
      },
      {
        format: 'email',
        errorMessage: t('register.form.email.validate.required'),
      }
    ],
    password: [
      {
        required: true,
        errorMessage: t('register.form.password.validate.required'),
      }
    ],
  }



  const handleSubmit = () => {
    formRef.value.validate().then(res => {

      userStore.login(formState.value).then(() => {
        emitter.emit('vanish');
        uni.reLaunch({
            url: '/pages/app/index',
        })
      });

   
    }).catch(errors => {
      uni.showToast({
        title: '倒计时结束后再发送'
      });
    })
  }

  return {
    formRef,
    formState,
    rules,
    handleSubmit
  }

}